package com.moli.iov.admin.service.impl;

import com.moli.iov.admin.constants.Constants;
import com.moli.iov.admin.model.vo.req.VehicleUnauthReqVo;
import com.moli.iov.admin.service.IVehicleUnauthFacadeService;
import com.moli.iov.base.model.dto.req.VehicleUnauthReqDto;
import com.moli.iov.base.model.dto.res.VehicleUnauthResDto;
import com.moli.iov.base.remote.IVehicleUnauthRemoteService;
import com.moli.iov.model.dto.req.PageExtend;
import com.moli.iov.rest.RestResponse;
import com.moli.iov.util.CommonUtils;
import com.moli.iov.util.ExcelUtil;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 未授权登录管理
 * </p>
 *
 * @author zql
 * @since 2020/2/20 16:40
 */
@Slf4j
@Service
public class VehicleUnauthFacadeServiceImpl implements IVehicleUnauthFacadeService {

  @Reference(group = "${service.group}", check = false)
  private IVehicleUnauthRemoteService vehicleUnauthRemoteService;

  /**
   * 分页查询 未授权登录
   * @param vehicleUnauthReqVo
   * @return
   */
  @Override
  public RestResponse<PageExtend<VehicleUnauthResDto>> listPageVehicleUnauth(
      VehicleUnauthReqVo vehicleUnauthReqVo) {
    return vehicleUnauthRemoteService.listPageVehicleUnauth(convert(vehicleUnauthReqVo));
  }

  /**
   * 处理
   * @param vehicleUnauthReqVo
   * @return
   */
  @Override
  public RestResponse updateVehicleUnauthOfProcess(VehicleUnauthReqVo vehicleUnauthReqVo) {
    return vehicleUnauthRemoteService.updateVehicleUnauthOfProcess(convert(vehicleUnauthReqVo));
  }

  /**
   * 导出
   * @param vehicleUnauthReqVo
   * @param request
   * @param res
   */
  @Override
  public void export(VehicleUnauthReqVo vehicleUnauthReqVo, HttpServletRequest request,
      HttpServletResponse res) throws Exception {
    vehicleUnauthReqVo.setPageNum(Constants.EXPORT_PAGE_NUM);
    vehicleUnauthReqVo.setPageSize(Constants.EXPORT_PAGE_SIZE);
    RestResponse<PageExtend<VehicleUnauthResDto>> restResponse = vehicleUnauthRemoteService
        .listPageVehicleUnauth(convert(vehicleUnauthReqVo));
    if(!restResponse.flag || restResponse.getData() == null){
      log.info("导出失败");
      return;
    }

    final String[] propNames = {"vin","companyName", "iccid", "deviceId"
        ,"createTime", "statusStr","resultStr","loginAuthStr", "processTime"};
    final String[] headStrs = {"车架号","车辆所属公司", "ICCID","T-BOXID", "登录时间","处理状态",
        "处理结果","登入授权", "处理时间"};
    String title = "未授权登录";
    List<VehicleUnauthResDto> list = restResponse.getData().getRecords();
    ExcelUtil.exportDataExcel(request, res, list, propNames, headStrs, title);
  }

  /**
   * 转换成 VehicleCommandDto
   * @param vehicleUnauthReqVo
   * @return
   */
  private VehicleUnauthReqDto convert(VehicleUnauthReqVo vehicleUnauthReqVo){
    VehicleUnauthReqDto vehicleUnauthReqDto = new VehicleUnauthReqDto();
    CommonUtils.copyProperties(vehicleUnauthReqVo, vehicleUnauthReqDto);
    return vehicleUnauthReqDto;
  }
}
